#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QTextEdit>
#include <QProgressBar>
#include <QListView>
#include <QStandardItemModel>
#include <QMessageBox>
#include <QDateTime>
#include <QTimer> // 替换sleep

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QWidget window;
    window.setWindowTitle("微电网能源管理系统 (纯代码版)");
    window.resize(800, 600);

    QVBoxLayout *mainLayout = new QVBoxLayout(&window);

    // 顶部按钮栏
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    QPushButton *btnAddDiesel = new QPushButton("添加柴油发电机");
    QPushButton *btnAddPV = new QPushButton("添加光伏系统");
    QPushButton *btnAddBattery = new QPushButton("添加储能电池");
    QPushButton *btnAddLoad = new QPushButton("添加负载");
    QPushButton *btnAddGrid = new QPushButton("添加电网");
    QPushButton *btnRunSimulation = new QPushButton("运行仿真");

    buttonLayout->addWidget(btnAddDiesel);
    buttonLayout->addWidget(btnAddPV);
    buttonLayout->addWidget(btnAddBattery);
    buttonLayout->addWidget(btnAddLoad);
    buttonLayout->addWidget(btnAddGrid);
    buttonLayout->addWidget(btnRunSimulation);

    // 系统状态栏
    QHBoxLayout *statusLayout = new QHBoxLayout();
    QLabel *labelStatus = new QLabel("系统状态: 就绪");
    QProgressBar *powerProgressBar = new QProgressBar();
    powerProgressBar->setRange(0, 500);
    powerProgressBar->setValue(0);
    powerProgressBar->setFormat("总功率: %v kW");

    statusLayout->addWidget(labelStatus);
    statusLayout->addWidget(powerProgressBar);

    // 中部内容区域
    QHBoxLayout *contentLayout = new QHBoxLayout();
    QListView *componentListView = new QListView();
    QStandardItemModel *listModel = new QStandardItemModel(componentListView);
    componentListView->setModel(listModel);

    QTextEdit *textOutput = new QTextEdit();
    textOutput->setReadOnly(true);

    contentLayout->addWidget(componentListView, 1);
    contentLayout->addWidget(textOutput, 2);

    // 组装主界面
    mainLayout->addLayout(buttonLayout);
    mainLayout->addLayout(statusLayout);
    mainLayout->addLayout(contentLayout);

    // 信号槽连接
    QObject::connect(btnAddDiesel, &QPushButton::clicked, [&]() {
        QStandardItem *newItem = new QStandardItem(QIcon(":/icons/diesel.png"), "柴油发电机");
        listModel->appendRow(newItem);
        textOutput->append("[" + QDateTime::currentDateTime().toString() + "] 已添加柴油发电机");
        powerProgressBar->setValue(powerProgressBar->value() + 100);
    });

    QObject::connect(btnAddPV, &QPushButton::clicked, [&]() {
        QStandardItem *newItem = new QStandardItem(QIcon(":/icons/pv.png"), "光伏系统");
        listModel->appendRow(newItem);
        textOutput->append("[" + QDateTime::currentDateTime().toString() + "] 已添加光伏系统");
        powerProgressBar->setValue(powerProgressBar->value() + 50);
    });

    // 修复：使用QTimer代替阻塞式sleep
    QObject::connect(btnRunSimulation, &QPushButton::clicked, [&]() {
        textOutput->append("[" + QDateTime::currentDateTime().toString() + "] 开始运行仿真...");
        labelStatus->setText("系统状态: 仿真中...");

        // 使用定时器实现非阻塞仿真
        QTimer *timer = new QTimer();
        int progress = 0;

        QObject::connect(timer, &QTimer::timeout, [&]() {
            progress += 10;
            powerProgressBar->setValue(progress * 5);

            if (progress >= 100) {
                timer->stop();
                timer->deleteLater();
                textOutput->append("[" + QDateTime::currentDateTime().toString() + "] 仿真完成!");
                labelStatus->setText("系统状态: 仿真完成");
            }
        });

        timer->start(100); // 每100ms触发一次
    });

    window.show();
    return app.exec();
}
